package design.pattern.Lu20StatePattern;

/**
 * @program JavaBase
 * @description: 状态模式
 * @author: zhanglu
 * @create: 2020-05-17 19:53:00
 */
public class StatePattern01 {

    public static void main(String[] args) {
        Context context = new Context();
        context.setLiftState(new ClosingState());
        context.open();
    }

}

abstract class LiftState{
    protected Context context;

    public void setContext(Context context){
        this.context = context;
    }

    protected abstract void open();
    protected abstract void close();
    protected abstract void run();
    protected abstract void stop();
}

class OpenningState extends LiftState{

    @Override
    protected void open() {
        System.out.println("open");
    }

    @Override
    protected void close() {
        super.context.setLiftState(Context.closingState);
        super.context.getLiftState().close();
    }

    @Override
    protected void run() {

    }

    @Override
    protected void stop() {

    }
}

class ClosingState extends LiftState { //电梯门关闭，这是关闭状态要实现的动作 @Override
    public void close() {
        System.out.println("电梯门关闭...");
    }
    //电梯门关了再打开 @Override
    public void open() {
        super.context.setLiftState(Context.openningState); //置为敞门状态
        super.context.getLiftState().open();
    }
    //电梯门关了就运行，这是再正常不过了 @Override
    public void run() {
        super.context.setLiftState(Context.runningState); //设置为运行状态
        super.context.getLiftState().run();
    }
    //电梯门关着，我就不按楼层 @Override
    public void stop() {
        super.context.setLiftState(Context.stoppingState); //设置为停止状态
        super.context.getLiftState().stop();
    }
}
class RunningState extends LiftState { //电梯门关闭?这是肯定的
    @Override
    public void close() {
//do nothing
    } //运行的时候开电梯门?你疯了!电梯不会给你开的 @Override
    public void open() {
//do nothing
    } //这是在运行状态下要实现的方法 @Override
    public void run() {
        System.out.println("电梯上下运行...");
    } //这绝对是合理的，只运行不停止还有谁敢坐这个电梯?!估计只有上帝了 @Override
    public void stop() {
        super.context.setLiftState(Context.stoppingState);//环境设置为停止状态
        super.context.getLiftState().stop();
    }
}
class StoppingState extends LiftState { //停止状态关门?电梯门本来就是关着的!
    @Override
    public void close() {
//do nothing;
    } //停止状态，开门，那是要的! @Override
    public void open() {
        super.context.setLiftState(Context.openningState);
        super.context.getLiftState().open();
    } //停止状态再运行起来，正常得很 @Override
    public void run() {
        super.context.setLiftState(Context.runningState);
        super.context.getLiftState().run();
    } //停止状态是怎么发生的呢?当然是停止方法执行了 @Override
    public void stop() {
        System.out.println("电梯停止了...");
    }
}

class Context{
    public final static OpenningState openningState = new OpenningState();
    public final static ClosingState closingState = new ClosingState();
    public final static RunningState runningState = new RunningState();
    public final static StoppingState stoppingState = new StoppingState();

    private LiftState liftState;

    public void setLiftState(LiftState liftState){
        this.liftState = liftState;
        this.liftState.setContext(this);
    }

    public LiftState getLiftState(){
        return this.liftState;
    }

    public void open(){
        this.liftState.open();
    }
    public void close(){
        this.liftState.close();
    }
    public void run(){
        this.liftState.run();
    }
    public void stop(){
        this.liftState.stop();
    }

}

